// 566. 重塑矩阵

function matrixReshape(mat: number[][], r: number, c: number): number[][] {
  const row = mat.length,
    col = mat[0].length;
  if (row * col !== r * c) return mat;

  const newMat: number[][] = new Array(r)
    .fill(0)
    .map(() => new Array(c).fill(0));
  for (let i = 0; i < row * col; i++) {
    newMat[Math.floor(i / c)][i % c] = mat[Math.floor(i / col)][i % col];
  }

  return newMat;
}

const mat = [
    [1, 2],
    [3, 4],
  ],
  r = 1,
  c = 4;

console.log(matrixReshape(mat, r, c));
